<template>
    <!-- 布局容器 -->
    <el-container class="home-container">
        <!-- 头部区域 -->
        <el-header>
            <div>
                <img class="home-logo" src="../assets/sys_build.png" alt="">
                <span>管理系统</span>
            </div>
            <el-button type="info" @click="logout">退出</el-button>
        </el-header>
        <!-- 中间区域 -->
        <el-container>
            <!-- 侧边栏区域 -->
            <el-aside :width="isCollapse ? '64px' : '200px'">
                <div class="toggle-button" @click="toggleCollapse" :width="isCollapse ? '64px' : '200px'">
                    <el-icon v-show="isCollapse">
                        <Expand />
                    </el-icon>
                    <el-icon v-show="isCollapse ? false : true">
                        <Fold />
                    </el-icon>
                </div>
                <el-col>
                    <el-menu background-color="#333744" text-color="#fff" active-text-color="#409eff"
                        unique-opened="true" :collapse="isCollapse" :collapse-transition="false" router :default-active="activePath">
                        <!-- 一级菜单 -->
                        <el-sub-menu :index="item.link" v-for="item in menuList" :key="item.id">
                            <!-- 一级菜单 模板 -->
                            <template #title>
                                <!-- 图标 -->
                                <i :class="iconsObj[item.id]"></i>
                                <!-- 文本 -->
                                <span>{{ item.name }}</span>
                            </template>
                            <!-- 二级菜单 -->
                            <el-menu-item :index="subItem.link" v-for="subItem in item.secondItems"
                                :key="subItem.id" @click="saveNaviState(subItem.link)">
                                <template #title>
                                    <!-- 图标 -->
                                    <el-icon class="no-inherit">
                                        <Menu />
                                    </el-icon>
                                    <!-- 文本 -->
                                    <span>{{ subItem.name }}</span>
                                </template></el-menu-item>
                        </el-sub-menu>
                    </el-menu>
                </el-col>
            </el-aside>
            <!-- 主体区域 -->
            <el-main><router-view></router-view></el-main>
        </el-container>
    </el-container>
</template>
<script>
export default {
    data() {
        return {
            // 左侧菜单列表
            menuList: [],
            iconsObj: {
                1: 'iconfont icon-baobiao',
                4: 'iconfont icon-danju',
                6: 'iconfont icon-user',
                8: 'iconfont icon-tijikongjian',
                11: 'iconfont icon-shangpin'
            },
            // 默认不折叠
            isCollapse: false,
            // 被激活的链接地址
            activePath: ''
        }
    },
    created() {
        this.getMenuList()
        this.activePath = window.sessionStorage.getItem('activePath')
    },
    methods: {
        logout() {
            window.sessionStorage.clear()
            this.$router.push('/login')
        },
        // 获取菜单列表
        getMenuList() {
            this.$http.get('/menuItems/allitems').then(res => {
                console.log(res)
                if (res.data.success === true) {
                    this.menuList = res.data.obj
                } else {
                    this.$message.error('获取菜单列表失败')
                }
            }).catch(err => {
                this.$message.error('获取菜单列表失败' + err)
            })
        },
        // 切换侧边栏展开折叠状态
        toggleCollapse() {
            this.isCollapse ^= true
        },
        // 保存链接到激活状态
        saveNaviState(activePath) {
            window.sessionStorage.setItem('activePath', activePath)
            this.activePath = activePath
        }
    }
}
</script>
<style lang="less" scoped>
.el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 10px;
    align-items: center;
    color: #fff;
    font-size: 20px;

    >div {
        display: flex;
        align-items: center;

        span {
            padding-left: 15px;
        }
    }
}

.home-logo {
    width: 80px;
    height: 45px;
}

.el-aside {
    background-color: #333744;
}

.el-main {
    background-color: #eaedf1;
}

.home-container {
    height: 100%;
}

.iconfont {
    padding-right: 10px;
}

.toggle-button {
    background-color: #4a5064;
    color: #fff;
    font-size: 22px;
    line-height: 28px;
    cursor: pointer;
    text-align: center;
}
</style>
